<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户列表</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=312" />
    <style>
        .layui-form-item {
            margin-bottom: 25px;
        }

        label .layui-icon {
            font-size: 14px;
            color: #666;
        }
    </style>
</head>

<body>

    <!-- 加载动画 -->
    <div class="page-loading">
        <div class="ball-loader">
            <span></span><span></span><span></span><span></span>
        </div>
    </div>

    <!-- 正文开始 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">

                <div class="layui-form toolbar">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">搜索用户：</label>
                            <div class="layui-input-inline mr0">
                                <input name="keyword" class="layui-input" type="text" placeholder="输入用户账号" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn icon-btn" lay-filter="formSubSearchTbBas" lay-submit>
                                <i class="layui-icon">&#xe615;</i>搜索
                            </button>
                            <!-- 下拉按钮 -->
                            <div class="dropdown-menu dropdown-hover">
                                <button class="layui-btn icon-btn">
                                    &nbsp;更多 <i class="layui-icon layui-icon-drop"></i></button>
                                <ul class="dropdown-menu-nav">
                                    <li><a id="btn_alldel"><i class="layui-icon layui-icon-delete"></i>全部清空</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>

                <table class="layui-table" id="tableTbBas" lay-filter="tableTbBas"></table>
            </div>
        </div>
    </div>
    <!-- 表格操作列 -->
    <script type="text/html" id="tableBarTbBas">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="pay">充值</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="vip">开通VIP</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改信息</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    
</script>
    <!-- js部分 -->
    <script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
    <script type="text/javascript" src="/static/assets/js/common.js?v=312"></script>
    <script>
        layui.use(['layer', 'jquery', 'table', 'dropdown', 'form'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var table = layui.table;
            var dropdown = layui.dropdown;
            var form = layui.form;
            // 渲染表格
            var insTb = table.render({
                elem: '#tableTbBas',
                url: "{:url('admin/user/index',['type'=>'get'])}",
                page: true,
                cols: [[
                    { field: 'Id', align: 'center', sort: true, title: 'UID', width: 90 },
                    { field: 'username', align: 'center', title: '用户账号', width: 120 },
                    {
                        field: 'grade', align: 'center', title: '会员等级', sort: true, templet: function (d) {
                            if (d.grade == '0') {
                                return "<span style='color: green;'>普通用户</span>";
                            } else if (d.grade == '1') {
                                return "<span style='color: red;'>VIP用户</span>";
                            } else {
                                return "<span style='color: #722ed1;'>什么？</span>";
                            }
                        }
                    },
                    { field: 'end_time', align: 'center', title: '会员结束时间' },
                    { field: 'invite', align: 'center', title: '上级id', width: 90 },
                    { field: 'balance', align: 'center', title: '积分余额(10积分/1元)' },
                    { align: 'center', toolbar: '#tableBarTbBas', title: '操作', minWidth: 250 }
                ]]
            });

            //监听工具条
            table.on('tool(tableTbBas)', function (obj) {
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event;
                if (layEvent === 'pay') { // 
                    layer.prompt({
                        formType: 0,
                        value: '10',
                        title: '输入充值积分数量',
                    }, function (value, index, elem) {

                        $.ajax({
                            type: "POST",                      //请求类型
                            url: "{:url('admin/user/pay')}",//URL
                            data: 'id=' + data.Id + '&price=' + value,   //传递的参数
                            dataType: "json",                 //返回的数据类型
                            success: function (data) {
                                if (data.code == 0) {
                                    layer.alert('充值成功');
                                    insTb.reload();
                                } else if (data.code == -1) {
                                    layer.alert('充值失败');
                                }
                            },
                            error: function (data) {
                                alert("请求失败");
                            }
                        });

                        layer.close(index);
                    });
                }
                if (layEvent === 'vip') { // 
                    layer.prompt({
                        formType: 0,
                        value: '1',
                        title: '请输入开通几个月',
                    }, function (value, index, elem) {

                        $.ajax({
                            type: "POST",                      //请求类型
                            url: "{:url('admin/user/open_vip')}",//URL
                            data: 'id=' + data.Id + '&time=' + value,   //传递的参数
                            dataType: "json",                 //返回的数据类型
                            success: function (data) {
                                if (data.code == 0) {
                                    layer.alert('开通或增加时间成功');
                                    insTb.reload();
                                } else if (data.code == -1) {
                                    layer.alert('开通失败');
                                }
                            },
                            error: function (data) {
                                alert("请求失败");
                            }
                        });

                        layer.close(index);
                    });
                }
                if (layEvent === 'edit') { // 改密
                    layer.open({
                        type: 2,
                        area: ['50%', '60%'],
                        content: "{:url('admin/user/edit_user')}" + "?id=" + data.Id,
                        end: function () {
                            insTb.reload();
                        }
                    });
                }
                if (layEvent === 'del') { // 删除
                    layer.open({
                        title: '删除确认'
                        , content: '将要用户：' + data.username + ' 删除 确定吗？'
                        , btn: ['确认', '取消']
                        , yes: function (index, layero) {
                            $.ajax({
                                type: "POST",                      //请求类型
                                url: "{:url('admin/user/index')}",//URL
                                data: 'type=del&id=' + data.Id,   //传递的参数
                                dataType: "json",                 //返回的数据类型
                                success: function (data) {
                                    if (data.code == 0) {
                                        layer.alert('删除成功');
                                        obj.del();
                                    } else if (data.code == -1) {
                                        layer.alert('删除失败');
                                    }
                                },
                                error: function (data) {
                                    alert("请求失败");
                                }
                            });
                            layer.closeAll();
                        }
                    });

                }
                dropdown.hideAll();
            });

            $('#btn_alldel').click(function () {
                layer.open({
                    title: '删除确认'
                    , content: '将要删除全部用户！确定吗？'
                    , btn: ['确认', '取消']
                    , yes: function (index, layero) {
                        $.ajax({
                            type: "POST",                      //请求类型
                            url: "{:url('admin/user/index')}",//URL
                            data: 'type=delall',   //传递的参数
                            dataType: "json",                 //返回的数据类型
                            success: function (data) {
                                if (data.code == 0) {
                                    layer.alert('清空成功！');
                                    insTb.reload();
                                } else if (data.code == -1) {
                                    layer.alert('清空失败');
                                }
                            },
                            error: function (data) {
                                alert("请求失败");
                            }
                        });
                        layer.closeAll();
                    }
                });
            });
            // 搜索按钮点击事件
            form.on('submit(formSubSearchTbBas)', function (data) {
                insTb.reload({ where: data.field }, 'data');
            });

            window.cha = function (d) {
                layer.open({
                    type: 2,
                    area: ['400px', '300px'],
                    content: "{:url('admin/invite/info')}" + "?id=" + d
                    
                });
            }
        });
    </script>
</body>

</html>